package cn.sh.ideal.web.autodiscover.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
public class AutoDiscoverController {
	
	private RestTemplate rs = new RestTemplate();
	
	
	@Value("${server_address}")
	private String serverAddress;
	
	/**
	 * 
	 * 查询指标规则
	 * 
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryIndexRule", method = RequestMethod.GET)
	@ResponseBody
	public String queryIndexRule(){
		String url = serverAddress + "/queryIndexRule";
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
		
		ResponseEntity<String> data = rs.exchange(url, HttpMethod.GET, entity, String.class);
		
		return data.getBody();
		
	}
	
	
	/**
	 * 
	 * 查询频率信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryFrequency", method = RequestMethod.GET)
	@ResponseBody
	public String queryFrequency(){
		String url = serverAddress + "/queryFrequency";
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
		
		ResponseEntity<String> data = rs.exchange(url, HttpMethod.GET, entity, String.class);
		
		return data.getBody();
		
	}
	
	
	
	
	/**
	 * 
	 * 查询运维信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/queryKpi", method = RequestMethod.GET)
	@ResponseBody
	public String queryKpi(){
		
		String url = serverAddress + "/queryKpi";
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
		
		ResponseEntity<String> data = rs.exchange(url, HttpMethod.GET, entity, String.class);
		
		return data.getBody();
		
		
		
		
	}
	
	/**
	 * 返回prometheus接口获取的ips与库里ip的差集ip
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getIps", method = RequestMethod.GET)
	@ResponseBody
	public String getIps(){
		
		String url = serverAddress + "/getIps";
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType
				.parseMediaType("application/json;charset=UTF-8");
		headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);
		
		ResponseEntity<String> data = rs.exchange(url, HttpMethod.GET, entity, String.class);
		
		return data.getBody();
		
		
		
		
	}
	
	

	/**
	 * 保存数据
	 * 
	 * @param object1
	 * @return
	 */
	@RequestMapping(value = "/saveInfo", method = RequestMethod.POST)
	@ResponseBody
	public String saveInfo(@RequestBody String object1){
		String url = serverAddress + "/saveInfo";
		HttpHeaders headers = new HttpHeaders();
		
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		
		MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
		
		params.add("msg", object1);
		HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params,
				headers);

		ResponseEntity<String> data = rs.exchange(url, HttpMethod.POST, requestEntity, String.class);
		
		return data.getBody();
		
		
		
	}
	
	
}
